package com.house365.core.adapter;

import java.util.ArrayList;
import java.util.List;

import com.house365.core.application.BaseApplication;
import com.house365.core.image.AsyncImageLoader;
import com.house365.core.image.CacheImageUtil;
import com.house365.core.image.CacheImageUtil.ImageOperate;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
 * @author py
 * @date 2012-12-03
 * @comment
 */
public abstract class BaseCacheListFragmentPagerAdapter<T> extends FragmentPagerAdapter {
	protected BaseApplication application;
	protected AsyncImageLoader mAil;
	protected Context context;

	public BaseCacheListFragmentPagerAdapter(Context context,FragmentManager fm) {
		super(fm);
		this.list = new ArrayList<T>();
		this.context = context;
		application = (BaseApplication) context.getApplicationContext();
		mAil = new AsyncImageLoader(context);
	}
	@Override
	public Fragment getItem(int position) {
		return getAdapterFragment(this, position);
	}
	
	
	
	public abstract Fragment getAdapterFragment(FragmentPagerAdapter adapter,int position);
	public void setCacheImage(final ImageView imageView,String imageUrl,final int resId,int scaleType) {
		CacheImageUtil.setCacheImage(imageView, imageUrl, resId, scaleType, mAil);
	}
	public void setCacheImageWithImageOper(final ImageView imageView,String imageUrl,final int resId,int scaleType,ImageOperate imageOperate){
		CacheImageUtil.setCacheImageWithImageOper(imageView, imageUrl, imageOperate , context.getResources(), resId, scaleType, mAil);
	}
	public void setCacheImageWithoutDef(final ImageView imageView,String imageUrl,int scaleType){
		CacheImageUtil.setCacheImageWithoutDef(imageView, imageUrl, scaleType, mAil);
	}
	protected final List<T> list;

	@Override
	public int getCount() {
		return list == null ? 0 : list.size();
	}

	public boolean addAll(List<? extends T> list) {
		return this.list.addAll(list);
	}

	public void clear() {
		this.list.clear();
	}

	public T getListItem(int i) {
		return this.list.get(i);
	}

	public long getItemId(int id) {
		return id;
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isEmpty() {
		return list == null ? true : list.isEmpty();
	}

	public T remove(int i) {
		return list.remove(i);
	}

	public void addItem(T t) {
		this.list.add(t);
	}

	public List<T> getList() {
		return list;
	}
}
